const express = require('express')

const route = express.Router()
//数据库文件
const adminModel = require('../db/model/adminModel')

//引入路由校验文件
const isLoginMiddle = require('../middleware/isLoginMiddle')
route.delete('/deleteAdmin',isLoginMiddle, async (req, res) => {
  //当收到请求 直接去数据库查询所有管理员信息
   const { adminName } = req.query
  //console.log(req.query,'req.query');
  console.log(adminName);
  try {
    //deleteOne返回一个对象,对象内有一个deletedCount属性为删除的个数
    const deleteResult = await adminModel.deleteOne({
      adminName
    })
    //console.log(deleteResult,'deleteResult');
    //判断删除的个数是否大于0
    if (deleteResult.deletedCount > 0) {
      return res.send({
        code:10000,
        mesage:'删除管理'+adminName+"成功",
        data:null
      }) 
    }
      res.send({
        code:10001,
        mesage:'删除管理'+adminName+'失败',
        data:null
      })  
  } catch (e) {
    console.log(e.message ,'删除管理异常');
    res.send({
      code:10003,
      mesage:'删除管理异常'+e.message,
      data:null
    })
  }
})

module.exports = route